<template>
	<view class="tab-bar">
		<view class="tab-box">
			<view  v-for="(tab, index) in pageList" :key="index" class="tab-bar-item" @click="switchTabBar(tab.pagePath)">
				<image class="tab_img2" v-if="index == 2" :src="currentPageRoute == tab.pagePath ? tab.selectedIconPath : tab.iconPath"></image>
				<image class="tab_img" v-else :src="currentPageRoute == tab.pagePath ? tab.selectedIconPath : tab.iconPath"></image>
				<view class="tab_text" :style="{color: currentPageRoute == tab.pagePath ? selectedColor : color}">{{tab.text}}</view>
			</view>
		</view>
		<view class="safe-area"></view>
	</view>
</template>

<script>
	import {
		config
	} from '@/utils/config.js'
	const iconBaseUrl = config.iconBaseUrl
	export default {
		name: "tab-bar",
		data() {
			return {
				tabbarBottom: 0,
				color: "#333333",
				selectedColor: "#333333",
				pageList: [{
						"pagePath": "pages/index/index",
						"iconPath": `${iconBaseUrl}/home.png`,
						"selectedIconPath": `${iconBaseUrl}/home_on.png`,
						"text": "首页"
					},
					{
						"pagePath": "pages/store/map",
						"iconPath": `${iconBaseUrl}/map.png`,
						"selectedIconPath": `${iconBaseUrl}/map_on.png`,
						"text": "找商家"
					},
					{
						"pagePath": "scan",
						"iconPath": `${iconBaseUrl}/scan.png`,
						"selectedIconPath": `${iconBaseUrl}/scan.png`,
						"text": "扫码"
					},
					{
						"pagePath": "pages/cart/index",
						"iconPath": `${iconBaseUrl}/cart.png`,
						"selectedIconPath": `${iconBaseUrl}/cart_on.png`,
						"text": "购物车"
					},
					{
						"pagePath": "pages/user/index",
						"iconPath": `${iconBaseUrl}/user.png`,
						"selectedIconPath": `${iconBaseUrl}/user_on.png`,
						"text": "我的"
					}
				]
			}
		},
		props: {
			isPlace: {
				type: Boolean,
				value: true
			}
		},
		computed: {
			currentPageRoute() {
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = routes[routes.length - 1].route //获取当前页面路由
				console.log(curRoute, 'curRoute')
				return curRoute
			}
		},
		beforeCreate() {
			console.log('onload*****')
		},
		mounted() {
			const fixSize = () => {
				const {
					safeAreaInsets,
					platform
				} = uni.getSystemInfoSync()

				console.log(uni.getSystemInfoSync())
				let barpx = 0
				if (safeAreaInsets && safeAreaInsets.bottom) {
					if (safeAreaInsets.bottom > 0) {
						barpx = safeAreaInsets.bottom
					} else {
						barpx = 0
					}
				}
				if (platform == "devtools") {
					this.tabbarBottom = barpx || 6
				} else if (platform == "ios") {
					this.tabbarBottom = barpx || 6
				} else if (platform == "android") {
					this.tabbarBottom = barpx || 10
				}
			}


			fixSize()
		},
		methods: {
			switchTabBar(pagePath) {
				if (pagePath == 'scan') {
					const token = uni.getStorageSync('token'); // 从缓存中获取token
					if (token) {
						uni.scanCode({
							success: function(res) {
								console.log(res);
								// if(res.path){
								// 	console.log(res.path.indexOf("pages/store/index"));
								// 	if (res.path.indexOf("pages/store/index") != -1) {
								// 		console.log(res.path);
								// 		uni.reLaunch({
								// 			url: '/'+res.path
								// 		})
								// 		// pages/store/index?scene=shopId%3D31%26inTell%3D1
								// 		return
								// 	}
								if(res.result){
									console.log(res.result)
										let arr=res.result.split('=')
										let num=arr[1]
										if(arr[0].indexOf('equipmenNo')!=-1){
										uni.reLaunch({
											url: '/pages/online/index?scene=equipmenNo%3D'+num
										})	
										}else{
											uni.reLaunch({
												url: '/pages/store/index?scene=shopId%3D'+num
											})
										}
										// pages/store/index?scene=shopId%3D31%26inTell%3D1
										return
								}

								// console.log('条码内容：' + res.path.replace('pages/online/index?scene=equipmenNo%3D',''));
								// if (res.path.indexOf("pages/online/index?scene=equipmenNo%3D") != -1) {
								// 	console.log('条码内容：' + res.path.replace(
								// 		'pages/online/index?scene=equipmenNo%3D', ''));
								// 	const equipmentNo = '' + res.path.replace(
								// 		'pages/online/index?scene=equipmenNo%3D', '') // res.resul
								// 	// const equipmentNo = 'RKE0500700' // res.resul
								// 	uni.setStorageSync('equipmentNo', equipmentNo)
								// 	uni.setStorageSync('BLUE_NAME', 'B6216') // 蓝牙名字
								// 	uni.setStorageSync('UUID_SERVICE',
								// 		'00010203-0405-0607-0809-0A0B0C0D1910') //服务UUID，设备方提供
								// 	uni.setStorageSync('UUID_CHAR',
								// 		'00010203-0405-0607-0809-0A0B0C0D2B10') //特征值UUID，设备方提供
								// 	uni.reLaunch({
								// 		url: `/pages/online/index?equipmentNo=${equipmentNo}`
								// 	})
								// } else {
								// 	const equipmentNo = '' + res.path.replace(
								// 		'pages/index/index?scene=equipmenNo%3D', '') // res.resul
								// 	// const equipmentNo = 'RKE0500700' // res.resul
								// 	uni.setStorageSync('equipmentNo', equipmentNo)
								// 	uni.setStorageSync('BLUE_NAME', 'B6216') // 蓝牙名字
								// 	uni.setStorageSync('UUID_SERVICE',
								// 		'00010203-0405-0607-0809-0A0B0C0D1910') //服务UUID，设备方提供
								// 	uni.setStorageSync('UUID_CHAR',
								// 		'00010203-0405-0607-0809-0A0B0C0D2B10') //特征值UUID，设备方提供
								// 	uni.reLaunch({
								// 		url: `/pages/online/index?equipmentNo=${equipmentNo}`
								// 	})
								// }
							}
						});
					} else {
						uni.redirectTo({
							url: "/pages/login/index"
						})
					}
				} else {
					if (pagePath == this.currentPageRoute) return
					uni.redirectTo({
						url: `/${pagePath}`
					})
				}

			}
		}
	};
</script>

<style lang="scss" scoped>
	.tab-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		// background-size: cover;
		// padding-bottom: env(safe-area-inset-bottom); // 适配iphoneX的底部
		z-index: 99999;
		
		.tab-box {
			display: flex;
			justify-content: center;
			align-items: flex-end;
			height: calc(139rpx);
			background: url('../../static/bg-tabbar.png') top center no-repeat;
			// background-size: 750rpx calc(139rpx);
			background-size: cover;
		}
		
		.safe-area {
			width: 100%;
			height: env(safe-area-inset-bottom);
			background: #fff;
		}

		.tab-bar-item {
			flex: 1;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			padding-bottom: 16rpx;

			.tab_img {
				width: 51rpx;
				height: 51rpx;
			}
			.tab_img2 {
				width: 88rpx;
				height: 88rpx;
			}

			.tab_text {
				font-size: 22rpx;
				// margin-top: 9rpx;
			}
		}
		.tab-bar-item:nth-of-type(3){
			transform: translateX(3rpx);
		}
	}
</style>
